<?php
/**
 * 首页控制器
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\Tongji;
use app\common\model\Type;
use app\common\model\User;
use Exception;
use think\Request;

class IndexController extends AdminBaseController {
	/**
	 * 后台首页
	 * @return string
	 * @throws Exception
	 */
	public function index(Request $request): string {

		return $this->fetch();
	}
	public function ceshi(Request $request) {
		$param = $request->param();

		$time_tongji_log = Tongji::with(['parent', 'user', 'type']);
		if (!empty($param['parent_id'])) {
			$time_tongji_log = $time_tongji_log->where('parent_id', $param['parent_id']);
		}
		if (!empty($param['type_id'])) {
			$time_tongji_log = $time_tongji_log->where('type_id', $param['type_id']);
		}
		if (!empty($param['user_id'])) {
			$time_tongji_log = $time_tongji_log->where('user_id', $param['user_id']);
		}
		if (!empty($param['create_time'])) {
			$time_range = explode(' - ', $param['create_time']);
			[$start_time, $end_time] = $time_range;
			$time_tongji_log = $time_tongji_log->whereBetweenTime('create_time', $start_time, $end_time);
		}
		$year_time = $time_tongji_log->field("from_unixtime(create_time,'%Y') as time,parent_id,type_id,user_id")->group('time')->select()->toArray();
		$month_time_list = $time_tongji_log->field("from_unixtime(create_time,'%Y-%m') as time,parent_id,type_id,user_id")->group('time')->select()->toArray();

		$ji_time_list = $time_tongji_log->field("QUARTER(from_unixtime(create_time,'%Y-%m-%d')) as time,from_unixtime(create_time,'%Y') as year,parent_id,type_id,user_id")->group('time')->select()->toArray();

		foreach ($year_time as $key => $value) {
			$num = Tongji::whereYear('create_time', $value['time'])->sum('num');
			$num3 = Tongji::whereYear('create_time', $value['time'])->sum('num');
			if (!empty($param['parent_id'])) {
				$num = Tongji::whereYear('create_time', $value['time'])->where('parent_id', $param['parent_id'])->sum('num');
			}
			if (!empty($param['type_id'])) {
				$num = Tongji::whereYear('create_time', $value['time'])->where('type_id', $param['type_id'])->sum('num');
			}
			if (!empty($param['user_id'])) {
				$num3 = Tongji::whereYear('create_time', $value['time'])->where('user_id', $param['user_id'])->sum('num');
			}
			$year_time[$key]['num'] = $num;
			$year_time[$key]['num3'] = $num3;
		}
		foreach ($month_time_list as $key => $value) {
			$last_year = date('Y-m', strtotime('- 1 year', strtotime($value['time'])));
			$num = Tongji::whereMonth('create_time', $value['time'])->sum('num');
			$num2 = Tongji::whereMonth('create_time', $last_year)->sum('num');
			$num3 = Tongji::whereMonth('create_time', $value['time'])->sum('num');
			if (!empty($param['parent_id'])) {
				$num = Tongji::whereMonth('create_time', $value['time'])->where('parent_id', $param['parent_id'])->sum('num');
				$num2 = Tongji::whereMonth('create_time', $last_year)->where('parent_id', $param['parent_id'])->sum('num');
			}
			if (!empty($param['type_id'])) {
				$num = Tongji::whereMonth('create_time', $value['time'])->where('type_id', $param['type_id'])->sum('num');
				$num2 = Tongji::whereMonth('create_time', $last_year)->where('type_id', $param['type_id'])->sum('num');
			}
			if (!empty($param['user_id'])) {
				$num3 = Tongji::whereMonth('create_time', $value['time'])->where('user_id', $param['user_id'])->sum('num');
			}
			$month_time_list[$key]['num'] = $num;
			$month_time_list[$key]['num2'] = $num2;
			$month_time_list[$key]['num3'] = $num3;
			if ($num2 == 0 || $num == 0) {
				$month_time_list[$key]['tong'] = 0;
				$month_time_list[$key]['huan'] = 0;
			} else {
				$month_time_list[$key]['tong'] = ($num - $num2) / $num2;
				$month_time_list[$key]['huan'] = (($num - $num2) / $num2) * 100;
			}

		}

		foreach ($ji_time_list as $key => $value) {
			if ($value['time'] == 1) {
				$start = $value['year'] . '-01-01';
				$end = $value['year'] . '-03-31';
			} else if ($value['time'] == 2) {
				$start = $value['year'] . '-04-01';
				$end = $value['year'] . '-06-30';
			} else if ($value['time'] == 3) {
				$start = $value['year'] . '-07-01';
				$end = $value['year'] . '-09-30';
			} else if ($value['time'] == 4) {
				$start = $value['year'] . '-10-01';
				$end = $value['year'] . '-12-31';
			}
			$num = Tongji::whereBetweenTime('create_time', $start, $end)->sum('num');
			$num3 = Tongji::whereBetweenTime('create_time', $start, $end)->sum('num');
			if (!empty($param['parent_id'])) {
				$num = Tongji::whereBetweenTime('create_time', $start, $end)->where('parent_id', $param['parent_id'])->sum('num');
			}
			if (!empty($param['type_id'])) {
				$num = Tongji::whereBetweenTime('create_time', $start, $end)->where('type_id', $param['type_id'])->sum('num');
			}
			if (!empty($param['user_id'])) {
				$num3 = Tongji::whereBetweenTime('create_time', $start, $end)->where('user_id', $param['user_id'])->sum('num');
			}
			$ji_time_list[$key]['num'] = $num;
			$ji_time_list[$key]['num3'] = $num3;
			$ji_time_list[$key]['time'] = $value['year'] . '年第' . $value['time'] . '季度';
		}
		$this->assign($request->get());
		$this->assign([
			'year_time' => json_encode(array_column($year_time, 'time')),
			'year_list' => json_encode(array_column($year_time, 'num')),
			'year_list_user' => json_encode(array_column($year_time, 'num3')),
			'year_data' => $year_time,
			'month_time' => json_encode(array_column($month_time_list, 'time')),
			'month_list' => json_encode(array_column($month_time_list, 'num')),
			'tong_list' => json_encode(array_column($month_time_list, 'tong')),
			'huan_list' => json_encode(array_column($month_time_list, 'huan')),
			'month_data' => $month_time_list,
			'ji_time' => json_encode(array_column($ji_time_list, 'time')),
			'ji_list' => json_encode(array_column($ji_time_list, 'num')),
			'ji_list_user' => json_encode(array_column($ji_time_list, 'num3')),
			'ji_data' => $ji_time_list,
			'user_list' => User::select(),
			'type_list' => Type::where('parent_id', '<>', 0)->select(),
			'parent_list' => Type::where('parent_id', 0)->select(),
		]);
		return $this->fetch();
	}
	public function array_do3($array1, $array2) {
		$mergedArray = [];

		// Create a map for the first array using type_id as key
		$array1Map = [];
		foreach ($array1 as $item) {
			$typeId = $item['type_id'];
			$array1Map[$typeId] = $item;
		}

		// Merge arrays based on type_id and only keep the merged ones
		foreach ($array2 as $item) {
			$typeId = $item['type_id'];
			if (isset($array1Map[$typeId])) {
				$mergedArray[] = array_merge($array1Map[$typeId], $item);
			}
		}

		return $mergedArray;
	}
	public function array_do($array) {
		$result = [];

		foreach ($array as $item) {
			$type_id = $item['type_id'];
			if (!isset($result[$type_id])) {
				$result[$type_id] = $item;
			} else {
				$result[$type_id]['num'] += $item['num'];
			}
		}

		$result = array_values($result);
		return $result;
	}
	public function array_do4($array) {
		$result = [];

		foreach ($array as $item) {
			$type_id = $item['type_id'];
			if (!isset($result[$type_id])) {
				$result[$type_id] = $item;
			} else {
				$result[$type_id]['num'] = $result[$type_id]['num'] . ',' . $item['num'];
			}
		}

		$result = array_values($result);
		return $result;
	}
	//public function ceshi() {
	//$this->send_sms('2039672', ['竞标活动'], ['15952177196']);
	//$this->send_sms('2039667', ['竞标活动'], ['15952177196']);
	//$this->send_sms('2039664', ['竞标活动', '2024年1月5日11:53:43'], ['15952177196', '17605205996']);
	//}
}